package com.zy.mc_module.wheel.recyclerview;

import androidx.recyclerview.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.zy.mc_module.base.App;
import com.zy.mc_module.utils.*;

import java.math.BigDecimal;

/**
 * Created on 18/10/23
 * BaseViewHolder
 */
public class BaseViewHolder extends RecyclerView.ViewHolder {
    private View mConvertView;
    private SparseArray<View> viewSparseArray;

    public BaseViewHolder(View itemView) {
        super(itemView);
        mConvertView = itemView;
        viewSparseArray = new SparseArray<>();
    }

    public static BaseViewHolder createViewHolder(View itemView) {
        return new BaseViewHolder(itemView);
    }

    public static BaseViewHolder createViewHolder(ViewGroup parent, int layoutId) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(layoutId, parent, false);
        return new BaseViewHolder(itemView);
    }

    /***
     * 根据Id 来获取 View
     */
    public <V extends View> V getView(int viewId) {
        View view = viewSparseArray.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            viewSparseArray.put(viewId, view);
        }
        return (V) view;
    }

    public View getConvertView() {
        return mConvertView;
    }

    public TextView getText(int id) {
        return getView(id);
    }

    public ImageView getImage(int id) {
        return getView(id);
    }

    public Button getBut(int id) {
        return getView(id);
    }

    /**
     * 订单有积分的时候设置金额
     */
    public void setOrderPrice(int id, int isExchange, Object price, String exchangeInfo) {
        TextView textView = getView(id);
        if (isExchange == 1) {
            //可以用积分兑换,显示积分
            textView.setText(exchangeInfo);
        } else {
            //显示金额
            textView.setText(("¥" + PriceUtils.decimalsT(price)));
        }
    }

    /**
     * 设置总价格
     */
    public void setAllPrice(int id, BigDecimal allPrice, BigDecimal allIntegral, String freight, String integralTypeName) {
        String str;
        TextView textView = getView(id);
        String price = "¥" + AppTools.decimalsT(ArithUtils.add(allPrice, freight));
        String integral = AppTools.removeTrim(allIntegral) + integralTypeName;
        if (allIntegral.intValue() == 0) {
            //积分为0
            str = price;
        } else if (allPrice.intValue() == 0) {
            //价格为0
            if (Double.valueOf(freight) == 0) {
                //运费为0
                str = integral;
            } else {
                str = price + "+" + integral;
            }
        } else {
            str = price + "+" + integral;
        }
        textView.setText((str));
    }

    /***
     * 设置金额,前面加符号 + -
     */
    public void setTextSym(int id, String str, String sym) {
        if ("0.00".equals(str.replace("¥", ""))) {
            getText(id).setText(str);
        } else {
            getText(id).setText(sym + str);
        }
    }

    /**
     * 设置图片
     */
    public void setImg(int id, int drawable) {
        GlideUtil.displayImage(getImage(id), drawable);
    }

    /**
     * 设置图片
     */
    public void setImg(int id, String path) {
        GlideUtil.displayImage(getImage(id), path);
    }

    /**
     * 设置图片
     */
    public void setImg(int id, String path, int errorImg) {
        GlideUtil.displayImage(getImage(id), path, errorImg);
    }

    /**
     * 设置控件文字
     */
    public void setText(int id, CharSequence charSequence) {
        if (null != charSequence) {
            getText(id).setText(charSequence);
        }
    }

    /**
     * 设置控件文字
     */
    public void setText(int id, int charSequence) {
        getText(id).setText(charSequence);
    }

    /**
     * 设置控件文字,如果内容为null,则隐藏
     */
    public void setTextVis(int id, CharSequence charSequence) {
        if (null == charSequence || charSequence.toString().contains("null")) {
            getText(id).setVisibility(View.GONE);
        } else {
            getText(id).setVisibility(View.VISIBLE);
            getText(id).setText(charSequence);
        }
    }

    public void setTextVis(int id, int con) {
        if (con == 0) {
            getText(id).setVisibility(View.GONE);
        } else {
            getText(id).setVisibility(View.VISIBLE);
            getText(id).setText(con + "");
        }
    }

    /**
     * 设置文字颜色
     */
    public void setTextColor(int id, int color) {
        getText(id).setTextColor(color);
    }

    /**
     * 获取控件文字
     */
    public String getTextCon(int id) {
        return getText(id).getText().toString().trim();
    }

    /**
     * 设置控件文字和颜色
     */
    public void setTextC(int id, CharSequence charSequence, int color) {
        getText(id).setText(charSequence);
        getText(id).setTextColor(color);
    }

    /***
     * 设置控件文字和图片
     * @param id
     * @param charSequence 文本信息
     * @param drawable 图片
     * @param orientation 图片位置 1左/2上/3右/4下
     */
    public void setTextD(int id, CharSequence charSequence, int drawable, int orientation) {
        getText(id).setText(charSequence);
        switch (orientation) {
            case 1:
                getText(id).setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0);
                break;

            case 2:
                getText(id).setCompoundDrawablesWithIntrinsicBounds(0, drawable, 0, 0);
                break;

            case 3:
                getText(id).setCompoundDrawablesWithIntrinsicBounds(0, 0, drawable, 0);
                break;

            case 4:
                getText(id).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, drawable);
                break;
        }
    }

    /**
     * 设置控件是否显示
     */
    public void setVisible(int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? view.VISIBLE : View.GONE);
    }

    /**
     * 设置控件是否显示
     */
    public void setGone(int viewId, boolean gone) {
        View view = getView(viewId);
        view.setVisibility(gone ? view.VISIBLE : View.INVISIBLE);
    }

    /**
     * 设置控件宽度
     */
    public void setWidth(int viewId, int width) {
        View view = getView(viewId);
        ViewGroup.LayoutParams para = view.getLayoutParams();
        para.width = width;
        view.setLayoutParams(para);
    }

    /**
     * 设置控件高度
     *
     * @param height -1铺满/-2包裹
     */
    public void setHeight(int viewId, int height) {
        View view = getView(viewId);
        ViewGroup.LayoutParams para = view.getLayoutParams();
        para.height = height;
        view.setLayoutParams(para);
    }

    /**
     * 设置控件宽高
     */
    public void setWidthHeight(int viewId, int width, int height) {
        View view = getView(viewId);
        ViewGroup.LayoutParams para = view.getLayoutParams();
        para.width = width;
        para.height = height;
        view.setLayoutParams(para);
    }

    /**
     * 设置控件padding
     */
    public void setPadding(int viewId, int left, int top, int right, int bottom) {
        View view = getView(viewId);
        view.setPadding(
                SizeUtils.dp2px(App.getAppContext(), left),
                SizeUtils.dp2px(App.getAppContext(), top),
                SizeUtils.dp2px(App.getAppContext(), right),
                SizeUtils.dp2px(App.getAppContext(), bottom));
    }

    /**
     * 设置控件间隔
     */
    public void setMargin(int viewId, int l, int t, int r, int b) {
        View view = getView(viewId);
        if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
            p.setMargins(l, t, r, b);
            view.requestLayout();
        }
    }

    /**
     * 设置子控件的点击事件
     */
    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        if (listener != null) {
            view.setOnClickListener(listener);
        }
    }

    /**
     * 设置父控件的点击事件
     */
    public void setOnClickListener(View.OnClickListener listener) {
        if (listener != null) {
            mConvertView.setOnClickListener(listener);
        }
    }

    /**
     * 设置长按控件的点击事件
     */
    public void setOnLongClickListener(View.OnLongClickListener listener) {
        if (listener != null) {
            mConvertView.setOnLongClickListener(listener);
        }
    }
}
